package io.renren.modules.PCguanli.entity;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelExporter {

    public void exportToExcel(List<ViolationRecordsEntity> records, String excelFilePath) throws IOException {
        // 创建一个新的工作簿
        Workbook workbook = new XSSFWorkbook();

        // 创建一个工作表
        Sheet sheet = workbook.createSheet("Violation Records");

        // 创建表头
        String[] headers = {"记录编号", "司机账号", "助手账号","驻场人员账号", "违规时间","违规地点","异常事实", "违规处罚"};
        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            Cell cell = headerRow.createCell(i);
            cell.setCellValue(headers[i]);
        }

        // 填充数据
        int rowNum = 1;
        for (ViolationRecordsEntity record : records) {
            Row row = sheet.createRow(rowNum++);

            row.createCell(0).setCellValue(record.getRecordid());
            row.createCell(1).setCellValue(record.getUserid());
            row.createCell(2).setCellValue(record.getDatcherid());
            row.createCell(3).setCellValue(record.getUseridmange());
            row.createCell(4).setCellValue(record.getTime().toString());
            row.createCell(5).setCellValue(record.getPlace());
            row.createCell(6).setCellValue(record.getDescription());
            row.createCell(4).setCellValue(record.getPenalties());
        }

        // 将数据写入到文件
        try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {
            workbook.write(outputStream);
        }

        // 关闭工作簿
        workbook.close();
    }
}